package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketRegion;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.AdminRegionService;
import com.cskaoyan.market.service.impl.AdminRegionServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.MarketRegionVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/region/*")
public class AdminRegionController extends HttpServlet {

    AdminRegionService regionService = new AdminRegionServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replace = requestURI.replace(req.getContextPath() + "/admin/region/", "");
        switch (replace) {
            case "list":
                list(req,resp);
                break;
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<MarketRegionVO> regionVOList=regionService.list();
        Object o = ResponseUtil.okList(regionVOList);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));

    }
}
